import { defineStore } from 'pinia'
import http from '../utils/http'

export const useDeviceStore = defineStore('device', {
    state: () => ({
        devices: [],
        checkingAll: false,
        gettingScoreAll: false,
    }),
    getters: {
        onlineDevices() {
            return this.devices.filter((device) => device.online)
        },
        hasScoreDevices() {
            return this.onlineDevices.filter((device) => device.score !== -1)
        },
        sumScore() {
            const hasScoreDevices = this.hasScoreDevices
            if (hasScoreDevices.length === 0) return -1
            return hasScoreDevices.reduce((sum, device) => {
                return sum + device.score
            }, 0)
        },
        maxScore() {
            const hasScoreDevices = this.hasScoreDevices
            if (hasScoreDevices.length === 0) return -1
            const initial = hasScoreDevices[0].score
            return hasScoreDevices.reduce((max, device) => {
                return Math.max(max, device.score)
            }, initial)
        },
        minScore() {
            const hasScoreDevices = this.hasScoreDevices
            if (hasScoreDevices.length === 0) return -1
            const initial = hasScoreDevices[0].score
            return hasScoreDevices.reduce((min, device) => {
                return Math.min(min, device.score)
            }, initial)
        },
        avgScore() {
            const sum = this.sumScore
            if (sum === -1) return -1
            const average = sum / this.hasScoreDevices.length
            return average.toFixed(2)
        },
    },
    actions: {
        async initDevices(count) {
            const devices = this.devices
            this.checkingAll = false
            this.gettingScoreAll = false
            if (devices.length > 0) {
                devices.splice(0, devices.length)
            }
            for (let i = 0; i < count; i++) {
                devices.push({
                    id: i + 1,
                    score: -1,
                    online: false,
                    msg: '',
                    current: false,
                })
            }
        },
        async checkDevice(id) {
            const device = this.devices.find((device) => device.id === id)
            device.msg = '正在检测...'
            device.current = true
            if (!device) return false
            const res = await http.get('/check', {
                params: {
                    id,
                },
            })
            if (res.success) {
                device.online = true
                device.msg = ''
                device.current = false
                return true
            } else {
                device.score = -1
                device.online = false
                device.msg = '未找到设备'
                device.current = false
                return false
            }
        },
        async getScore(id) {
            const device = this.devices.find((device) => device.id === id)
            device.msg = '正在获取分数...'
            device.current = true
            if (!device) return false
            const res = await http.get('/score', {
                params: {
                    id,
                },
            })
            let success = false
            if (res.success) {
                device.score = res.score
                success = true
                device.msg = '获取分数成功'
            } else {
                device.score = -1
                device.msg = '获取分数失败'
            }
            device.current = false
            return success
        },
        async checkAll() {
            this.checkingAll = true
            const _checkNext = async (i) => {
                if (i >= this.devices.length || !this.checkingAll) {
                    this.checkingAll = false
                    return
                }
                await this.checkDevice(this.devices[i].id)
                _checkNext(i + 1)
            }
            _checkNext(0)
        },
        stopCheckAll() {
            this.checkingAll = false
        },
        async getScoreAll() {
            this.gettingScoreAll = true
            const _getScoreNext = async (i) => {
                if (i >= this.onlineDevices.length || !this.gettingScoreAll) {
                    this.gettingScoreAll = false
                    return
                }
                await this.getScore(this.onlineDevices[i].id)
                _getScoreNext(i + 1)
            }
            _getScoreNext(0)
        },
        stopGetScoreAll() {
            this.gettingScoreAll = false
        },
        async resetAll() {
            await http.get('/reset')
        },
    },
})
